package edu.cuit.avatar.service.bean;

import edu.cuit.avatar.dao.OrderDao;
import edu.cuit.avatar.po.Order;
import edu.cuit.avatar.po.Payment;
import edu.cuit.avatar.service.OrderService;
import edu.cuit.avatar.service.PaymentService;
import edu.cuit.avatar.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/14 10:44
 */
@Service
public class OrderServiceBean implements OrderService {
    @Resource private OrderDao dao;
    @Resource private UserService userService;
    @Resource private PaymentService paymentService;

    /**
     * REQUIRED: 当方法调用时，如果不存在当前事务，那么就创建事务；如果之前的方法已经存在事务了，那么就沿用当前的事务
     *      updateScore()加入createOrder()的事务; 任意方法抛出异常，整个事务回滚
     * @param order
     */
    @Transactional
    @Override
    public void createOrder(Order order) {
        //添加订单
        dao.insert(order);
        //更新用户积分
        userService.updateScore(order.getUserId(), 10);
//        var payment = new Payment();
//        payment.setOrderId(order.getId());
//        payment.setPayType(1);
        //该方法必须在已有事务中进行调用
        //paymentService.payment(payment);
    }
}
